<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Paging {
	private $_url = '';
	private $_page = 1;
	private $_size = 10;
	private $_count = 0;
	private $_pages = 0;
	private $_numbers = 10;
	
	////////////////////////////////////////////////////////////////////////////////
	public function __construct($count, $page = 1, $size = 10, $url = '', $numbers = 10) {
		$this->_count = $count;
		$this->_page = $page;
		$this->_size = $size;
		$this->_url = $url;
		$this->_numbers = $numbers;
		
		$this->_pages = ceil($this->_count / $this->_size);
	}
	
	////////////////////////////////////////////////////////////////////////////////
	public function draw() {
		if ($this->_pages < 2) { return;} ?>
		<div class="paging_wrapper">
			<table cellspacing="0" cellpadding="0" class="paging_table">
				<tr>
				<?php
				if ($this->_page > 1) { ?>
					<td><a href="<?php echo $this->_getUrl(1); ?>" class="paging_page_off">First</a></td>
					<td><a href="<?php echo $this->_getUrl($this->_page - 1); ?>" class="paging_page_off">Previous</a></td>
				<?php } else { ?>
					<td><div class="paging_page_disabled">First</div></td>
					<td><div class="paging_page_disabled">Previous</div></td>
				<?php }
				
				$start = floor(($this->_page - 1) / $this->_numbers) * $this->_numbers + 1;

				for ($i = $start, $cnt = 1; $i <= $this->_pages && $cnt <= $this->_numbers; $i++, $cnt++) {
					if ($i == $this->_page) { ?>
						<td><a href="<?php echo $this->_getUrl($i); ?>" class="paging_page_on"><?php echo $i; ?></a></td>
					<?php } else { ?>
						<td><a href="<?php echo $this->_getUrl($i); ?>" class="paging_page_off"><?php echo $i; ?></a></td>
					<?php }
				}

				if ($this->_page < $this->_pages) { ?>
					<td><a href="<?php echo $this->_getUrl($this->_page + 1); ?>" class="paging_page_off">Next</a></td>
					<td><a href="<?php echo $this->_getUrl($this->_pages); ?>" class="paging_page_off">Last</a></td>
				<?php } else { ?>
					<td><div class="paging_page_disabled">Next</div></td>
					<td><div class="paging_page_disabled">Last</div></td>
				<?php } ?>
				</tr>
			</table>
		</div>
		<?php
	}
	
	private function _getUrl($page_id) {
		return Tools::parse($this->_url, array('page_id' => $page_id));
	}
}
